// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   BulkSmsHTTPGateway.java

package cn.sendsms;

import java.io.IOException;
import java.net.URL;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package cn.sendsms:
//			HTTPGateway, TimeoutException, GatewayException, DeliveryStatuses, 
//			OutboundMessage, MessageStatuses, FailureCauses

public class BulkSmsHTTPGateway extends HTTPGateway
{

	private String username;
	private String password;
	Object SYNC_Commander;

	public BulkSmsHTTPGateway(String id, String username, String password, Logger logger)
	{
		super(id, logger);
		started = false;
		this.username = username;
		this.password = password;
		from = "";
		SYNC_Commander = new Object();
		attributes = 77;
	}

	void startGateway()
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		logger.info("Starting Gateway: " + gtwId);
		super.startGateway();
	}

	void stopGateway()
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		logger.info("Stopping Gateway: " + gtwId);
		super.stopGateway();
	}

	public float queryBalance()
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		List request = new ArrayList();
		request.add(new HTTPGateway.HttpHeader("username", username, false));
		request.add(new HTTPGateway.HttpHeader("password", password, false));
		String reqLine = "";
		int i = 0;
		for (int n = request.size(); i < n; i++)
		{
			if (i != 0)
				reqLine = reqLine + "&";
			reqLine = reqLine + ((HTTPGateway.HttpHeader)request.get(i)).key + "=" + ((HTTPGateway.HttpHeader)request.get(i)).value;
		}

		URL url = new URL("http://bulksms.vsms.net/eapi/user/get_credits/1/1.1?" + reqLine);
		List response;
		synchronized (SYNC_Commander)
		{
			response = HttpGet(url);
		}
		if (((String)response.get(0)).charAt(0) == '0')
			return Float.parseFloat(((String)response.get(0)).substring(((String)response.get(0)).indexOf('|') + 1));
		else
			return -1F;
	}

	public DeliveryStatuses queryMessage(String refNo)
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		List request = new ArrayList();
		request.add(new HTTPGateway.HttpHeader("username", username, false));
		request.add(new HTTPGateway.HttpHeader("password", password, false));
		request.add(new HTTPGateway.HttpHeader("batch_id", refNo, false));
		String reqLine = "";
		int i = 0;
		for (int n = request.size(); i < n; i++)
		{
			if (i != 0)
				reqLine = reqLine + "&";
			reqLine = reqLine + ((HTTPGateway.HttpHeader)request.get(i)).key + "=" + ((HTTPGateway.HttpHeader)request.get(i)).value;
		}

		URL url = new URL("http://bulksms.vsms.net/eapi/status_reports/get_report/2/2.0?" + reqLine);
		List response;
		synchronized (SYNC_Commander)
		{
			response = HttpGet(url);
		}
		if (((String)response.get(0)).indexOf("0|Results to follow") == 0)
		{
			StringTokenizer tokens = new StringTokenizer((String)response.get(2), ":");
			tokens.nextToken();
			deliveryErrorCode = Integer.parseInt(tokens.nextToken());
			switch (deliveryErrorCode)
			{
			case 11: // '\013'
				return DeliveryStatuses.DELIVERED;

			case 0: // '\0'
			case 10: // '\n'
			case 12: // '\f'
			case 63: // '?'
			case 64: // '@'
				return DeliveryStatuses.KEEPTRYING;
			}
			return DeliveryStatuses.ABORTED;
		} else
		{
			return DeliveryStatuses.UNKNOWN;
		}
	}

	boolean sendMessage(OutboundMessage msg)
		throws TimeoutException, GatewayException, IOException, InterruptedException
	{
		URL url = null;
		List request = new ArrayList();
		boolean ok = false;
		logger.debug("BulkSMS: sendMessage().");
		request.add(new HTTPGateway.HttpHeader("username", username, false));
		request.add(new HTTPGateway.HttpHeader("password", password, false));
		request.add(new HTTPGateway.HttpHeader("message", msg.getText(), false));
		request.add(new HTTPGateway.HttpHeader("msisdn", msg.getRecipient().substring(1), false));
		request.add(new HTTPGateway.HttpHeader("allow_concat_text_sms", "1", false));
		if (msg.getStatusReport())
			request.add(new HTTPGateway.HttpHeader("want_report", "1", false));
		if (msg.getFlashSms())
			request.add(new HTTPGateway.HttpHeader("msg_class", "0", false));
		if (msg.getFrom() != null && msg.getFrom().length() != 0)
			request.add(new HTTPGateway.HttpHeader("source_id", msg.getFrom(), false));
		else
		if (from != null && from.length() != 0)
			request.add(new HTTPGateway.HttpHeader("source_id", from, false));
		String reqLine = "";
		int i = 0;
		for (int n = request.size(); i < n; i++)
		{
			if (i != 0)
				reqLine = reqLine + "&";
			reqLine = reqLine + ((HTTPGateway.HttpHeader)request.get(i)).key + "=" + ((HTTPGateway.HttpHeader)request.get(i)).value;
		}

		url = new URL("http://bulksms.vsms.net/eapi/submission/send_sms/2/2.0?" + reqLine);
		List response;
		synchronized (SYNC_Commander)
		{
			response = HttpGet(url);
		}
		if (((String)response.get(0)).charAt(0) == '0')
		{
			StringTokenizer tokens = new StringTokenizer((String)response.get(0), "|");
			tokens.nextToken();
			tokens.nextToken();
			msg.setRefNo(tokens.nextToken());
			msg.setDispatchDate(new Date());
			msg.setGatewayId(gtwId);
			msg.setMessageStatus(MessageStatuses.SENT);
			ok = true;
		} else
		{
			StringTokenizer tokens = new StringTokenizer((String)response.get(0), "|");
			switch (Integer.parseInt(tokens.nextToken()))
			{
			case 22: // '\026'
				msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
				break;

			case 23: // '\027'
				msg.setFailureCause(FailureCauses.GATEWAY_AUTH);
				break;

			case 24: // '\030'
				msg.setFailureCause(FailureCauses.BAD_FORMAT);
				break;

			case 25: // '\031'
			case 26: // '\032'
			case 27: // '\033'
			case 28: // '\034'
				msg.setFailureCause(FailureCauses.NO_CREDIT);
				break;

			case 40: // '('
				msg.setFailureCause(FailureCauses.GATEWAY_FAILURE);
				break;
			}
			msg.setRefNo(null);
			msg.setDispatchDate(null);
			msg.setMessageStatus(MessageStatuses.FAILED);
			ok = false;
		}
		return ok;
	}
}
